package main;

import java.awt.AWTException;
import java.awt.Graphics;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

import render.Tracer;

public class ShowImage extends JPanel implements KeyListener, MouseMotionListener {
    private static BufferedImage image;
    private Tracer render;
    private Robot robot;
    private boolean resetsize = false;
    private boolean preview = true;
    int width = 500;
    int height = 500;
    int scale = 10;

    double rotx = 0.0;
    double roty = 0.0;
    double rotz = 0.0;

    int w;
    int h;

    Tracer tracer;

    public void paint( Graphics g ) {
	g.drawImage( image , 0 , 0 , width , height , this );
    }

    public ShowImage() {
	try {
	    robot = new Robot();
	} catch ( AWTException e ) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	
	tracer = new Tracer( width / scale , height / scale , 1000 , this );
	addKeyListener( this );
	addMouseMotionListener( this );
	setRender( tracer );
	image = getRender().getImage();
	setFocusable( true );
    }

    public void render() {
	while ( true ) {
	    render.renderRandom();
	    if ( resetsize ) {
		tracer.resetSize( width / scale , height / scale );
		resetsize = false;
		image = getRender().getImage();
	    }
	}
    }

    public void setRender( Tracer render ) {
	this.render = render;
    }

    public Tracer getRender() {
	return render;
    }

    public void setSize( int w , int h ) {
	width = w;
	height = h;
	resetsize = true;
	JFrame f = (JFrame) this.getParent().getParent().getParent().getParent();
	f.setSize( width , height );
    }

    @Override
    public void keyPressed( KeyEvent e ) {
	if ( e.getKeyCode() == KeyEvent.VK_LEFT ) {
	    roty += 0.2;
	    render.rotate( rotx , roty , rotz );
	    render.clearBuffers();

	}

	if ( e.getKeyCode() == KeyEvent.VK_RIGHT ) {
	    roty -= 0.2;
	    render.rotate( rotx , roty , rotz );
	    render.clearBuffers();

	}

	if ( e.getKeyCode() == KeyEvent.VK_UP ) {
	    rotx -= 0.2;
	    render.rotate( rotx , roty , rotz );
	    render.clearBuffers();
	    // render.increaseLight( 0.1 );
	    // System.out.println( render.getLight() );
	}
	if ( e.getKeyCode() == KeyEvent.VK_ESCAPE ) {
	    System.exit( 0 );
	}
	if ( e.getKeyCode() == KeyEvent.VK_DOWN ) {
	    rotx += 0.2;
	    render.rotate( rotx , roty , rotz );
	    render.clearBuffers();
	    // render.increaseLight( -0.1 );
	    // System.out.println( render.getLight() );
	}
	if ( e.getKeyCode() == KeyEvent.VK_W ) {
	    render.move( 0 , 10 );
	}
	if ( e.getKeyCode() == KeyEvent.VK_A ) {
	    render.move( -10 , 0 );
	}
	if ( e.getKeyCode() == KeyEvent.VK_S ) {
	    render.move( 0 , -10 );
	}
	if ( e.getKeyCode() == KeyEvent.VK_D ) {
	    render.move( 10 , 0 );
	}
	if ( e.getKeyCode() == KeyEvent.VK_SPACE ) {
	    if ( preview ) {
		scale = 1;
		setSize( 500 , 500 );
		preview = false;
	    } else {
		scale = 10;
		preview = true;
		setSize( 500 , 500 );

	    }
	}
    }

    @Override
    public void keyReleased( KeyEvent arg0 ) {
	// TODO Auto-generated method stub
	// System.out.println( "aa" );
    }

    @Override
    public void keyTyped( KeyEvent arg0 ) {
	// TODO Auto-generated method stub
	// System.out.println( "aa" );
    }

    @Override
    public void mouseDragged( MouseEvent arg0 ) {
	// TODO Auto-generated method stub

    }

    @Override
    public void mouseMoved( MouseEvent e ) {
	// System.out.println(rotx + " " + roty);
	// (e.getY() - (getY() + (width / 2) - 8))/50;
	// roty -= (e.getX() - (getX() + (height / 2) - 30))/50;
	//	
	// render.rotate(1,0,0,rotx);
	//	
	// robot.mouseMove( getX() + width / 2 , getY() + height / 2 );
    }

}